<?php
class WBBBoardWebsiteAdapter extends WebsiteAdapter {
	private $boardID = 0;
	
	public function matches($url) {
		$params = WebsiteUtil::getParams($url);
		return ($params['page'] != null && strtolower($params['page']) == 'board');
	}
	
	public function parse(Website $website) {
		$board = WebsiteUtil::getBoardByUrl($website->getURL());
		$this->boardID = $board->id;
		phpQuery::newDocumentHTML($website->getContent());
		$categoryID = intval($website->getParam("boardID"));
		$archive = new ArchiveBoardThread();
		$category = new ArchiveBoardCategory();
		$cat = $category->find("boardID = '".$board->id."' AND categoryID = '".intval($categoryID)."'");
		$container = pq(".breadCrumbs li:last a");
		$params = WebsiteUtil::getParams($container->attr("href"));
		if (array_key_exists("boardID", $params)) {
			$cat->parentCategoryID = intval($params["boardID"]);
			$cat->update();
		}
		$i = 0;
		
		foreach(pq("ul#boardList li.board div[id*=board]") as $container) {
			++$i;
			$container = pq($container);
			$category->unsetAttributes();
			$id = intval(str_replace("board", "", $container->attr("id")));
			$row = Yii::app()->db->createCommand("SELECT id FROM tbl_archive_board_category WHERE boardID = '".$board->id."' AND categoryID = '".intval($categoryID)."'")->queryRow();
			$parentCategoryID = $row['id'];
			$title = pq($container->find("a[id=boardLink".$id."]")).text();
			$description = pq($container->find("p.boardlistDescription")).text();
			
			//Erstelle
			$category->categoryID = $id;
			$category->boardID = $this->boardID;
			$category->title = $title;
			$category->description = $description;
			$category->parentCategoryID = $parentCategoryID;
			
			//Suche
			$found = $category->find("boardID = ".$boardID." AND categoryID = ".$id);
			if ($found != null) {
				if (strlen($title) > 0) $found->title = $title;
				if (strlen($description) > 0) $found->description = $description;
				if ($categoryID != 0) $found->parentCategoryID = $categoryID;
				$found->update();
			}else{
				$category->setIsNewRecord(true);
				$category->insert();
			}
		}
		
		$i = 0;
		foreach (pq("tr[id*=threadRow] td[class=columnTopic] div[id*=thread]") as $container) {
			$archive->unsetAttributes();
			++$i;
			$link = pq($container)->find("a");
			$title = $link->text();
			$params = WebsiteUtil::getParams($link->attr("href"));
			$id = (array_key_exists("threadID", $params)) ? intval($params['threadID']) : 0;
			$boardID = $this->boardID;
			$container2 = pq("tr[id=threadRow".$id."] td[class=columnTopic] p.light a");
			$params = WebsiteUtil::getParams($container2->attr("href"));
			$userID = (array_key_exists("userID", $params)) ? intval($params['userID']) : 0;
			
			//FÃ¼ge URL hinzu
			WebsiteUtil::addURL("index.php?page=User&userID=".$userID, false, $board);
			WebsiteUtil::addURL("index.php?page=Thread&threadID=".$id, false, $board);
			
			//Attribute setzen
			$archive->categoryID = $cat->id;
			$archive->userID = $userID;
			$archive->title = $title;
			$archive->threadID = $id;
			
			//Suche
			$found = $archive->find('threadID = "'.$archive->threadID.'"');
			if ($found != null) {
				if (strlen($title) > 0) $found->title = $title;
				if ($userID != 0) $found->userID = $userID;
				if ($categoryID != 0) $found->categoryID = $categoryID;
				$found->update();
			}else{
				$archive->setIsNewRecord(true);
				$archive->insert();
			}
		}
		
		foreach(pq(".contentFooter .pageNavigation ul li a") as $link) {
			WebsiteUtil::addURL(pq($link)->attr("href"), true, $board);
		}
	}
}
?>
